package com.juyou.rider.app.api.order;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.laituo.juyou.core.Const;
import com.laituo.juyou.core.exception.AdminServiceException;
import com.laituo.juyou.core.exception.AppServiceException;
import com.laituo.juyou.core.exception.ExceptionDefinition;
import com.laituo.juyou.core.exception.ServiceException;
import com.laituo.juyou.data.component.CacheComponent;
import com.laituo.juyou.data.domain.*;
import com.laituo.juyou.data.dto.order.OrderRiderDTO;
import com.laituo.juyou.data.dto.rider.RiderLoginDTO;
import com.laituo.juyou.data.enums.OrderStatusType;
import com.laituo.juyou.data.enums.RiderOrderStatusType;
import com.laituo.juyou.data.mapper.*;
import com.laituo.juyou.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.rmi.ServerException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @description: 骑手订单接口
 * @author: fy
 * @date: 2020/03/03 21:02
 **/
@Service
public class RiderWithDrawServiceImpl implements RiderWithDrawService {

    @Autowired
    private RiderMapper riderMapper;

    @Autowired
    private RiderWithDrawMapper riderWithDrawMapper;


    @Override
    public Page<RiderWithDrawDO> getRiderWithDrawPage(Integer pageNo, Integer pageSize, String riderName, String status) throws ServerException {
        Wrapper<RiderWithDrawDO> wrapper = new EntityWrapper();
        if (!StringUtils.isEmpty(riderName)) {
            wrapper.like("rider_name", riderName);
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }
        wrapper.orderBy("gmt_create", false);
        Integer count = riderWithDrawMapper.selectCount(wrapper);
        List<RiderWithDrawDO> riderWithDrawDOS = riderWithDrawMapper.selectPage(new RowBounds((pageNo - 1) * pageSize, pageSize), wrapper);
        return new Page<>(riderWithDrawDOS, pageNo, pageSize, count);
    }

    @Override
    public Boolean addWithDraw(Long riderId, BigDecimal withDraw) throws ServerException, AppServiceException {
        RiderDO riderDO = riderMapper.selectById(riderId);
        if (null == riderDO) {
            throw new AppServiceException(ExceptionDefinition.RIDER_NOT_EXIST);
        }
        if (riderDO.getIncome().compareTo(withDraw) == -1) {
            throw new AppServiceException(ExceptionDefinition.RIDER_WITH_DRAW_ERROR);
        }
        RiderWithDrawDO riderWithDrawDO = new RiderWithDrawDO();
        riderWithDrawDO.setCardNo(riderDO.getCardNo())
                .setRiderId(riderId.toString())
                .setRiderName(riderDO.getName())
                .setStatus("0")
                .setWithDraw(withDraw)
                .setGmtCreate(DateUtil.date());
        if (riderWithDrawMapper.insert(riderWithDrawDO) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean updateStatus(Long id, Long adminId,Long riderId, String status, BigDecimal withDraw) throws ServerException, AppServiceException {
        RiderDO riderDO = riderMapper.selectById(riderId);
        if (null == riderDO || riderDO.getIncome().compareTo(withDraw) == -1) {
            throw new AppServiceException(ExceptionDefinition.RIDER_WITH_DRAW_ERROR);
        }
        RiderWithDrawDO riderWithDrawDO = new RiderWithDrawDO();
        riderWithDrawDO
                .setStatus(status)
                .setUserId(adminId.toString())
                .setId(id);
        riderWithDrawDO.setGmtUpdate(DateUtil.date());
        if (riderWithDrawMapper.updateById(riderWithDrawDO) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public RiderDO queryBalanceById(Long riderId) throws ServerException {
        return riderMapper.selectById(riderId);
    }
}
